home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cocktail / puma.lha / puma / src / mod.puma < prev    next >
Text File  |  1992-09-25  |  24KB  |  963 lines

  1. /* Ich, Doktor Josef Grosch, Informatiker, 29.4.1991 */
  2.  
  3. TRAFO Mod
  4. TREE Tree
  5. PUBLIC DefMod ImplMod MacroMod
  6.  
  7. GLOBAL {
  8.  
  9. FROM Positions    IMPORT tPosition;
  10. FROM IO        IMPORT StdOutput, WriteS, WriteNl;
  11. FROM StringMem    IMPORT WriteString;
  12. FROM Idents    IMPORT tIdent, NoIdent;
  13. FROM Texts    IMPORT WriteText;
  14. FROM Sets    IMPORT IsElement, IsNotEqual, Minimum, Maximum, IsEmpty;
  15. FROM Semantics    IMPORT IdentifyVar, LookupClass;
  16. FROM Optimize    IMPORT NeedsTempo, NeedsMatch, NeedsNoFinale, GetRule;
  17. FROM Tree    IMPORT NoTree, tTree, Options, f, SourceFile, WI, WN;
  18.  
  19. VAR
  20.    RoutineKind    : (kProcedure, kFunction, kPredicate);
  21.    i, j        : CARDINAL;
  22.    ListCount    : INTEGER;
  23.    rule        ,
  24.    TheClass    ,
  25.    InFormals    ,
  26.    OutFormals    ,
  27.    ReturnFormals,
  28.    Decls    : tTree;
  29.    TheName    : tIdent;
  30.    TemposDone    : BOOLEAN;
  31.  
  32. PROCEDURE WriteLine (Line: tPosition);
  33.    BEGIN
  34.       IF Line.Line # 0 THEN
  35.      !(* line ! WN (Line.Line); @ "@ WriteS (f, SourceFile); @" *)@
  36.       END;
  37.    END WriteLine;
  38.  
  39. PROCEDURE Match (t, Formals: tTree);
  40.    VAR TreeName    : tIdent;
  41.    VAR Pattern    : tTree;
  42.    BEGIN
  43.       IF (t^.Kind = Tree.NoPattern) OR (Formals^.Kind # Tree.Formal) THEN RETURN; END;
  44.       Pattern := t^.OnePattern.Pattern;
  45.       CASE Pattern^.Kind OF
  46.       | Tree.Decompose: WITH Pattern^.Decompose DO
  47.         TreeName := Object^.Class.TypeDesc^.NodeTypes.TreeName^.TreeName.Name;
  48.         IF (Formals^.Formal.TypeDesc^.Kind = Tree.UserType) OR
  49.            IsNotEqual (Object^.Class.TypeDesc^.NodeTypes.Types, Formals^.Formal.TypeDesc^.NodeTypes.Types) THEN
  50.            IF Object^.Class.Extensions^.Kind = Tree.NoClass THEN    (* Low ? *)
  51.           !      IF (! ImplMod (Path); !^.Kind # ! 
  52.            ELSE
  53.           !      IF NOT ! WI (TreeName); !.IsType (! ImplMod (Path); !, ! 
  54.            END;
  55.            WI (TreeName); !.! WI (Object^.Class.Name); !) THEN EXIT; END;!
  56.         END;
  57.         Match (Patterns, Object^.Class.Formals);
  58.      END;
  59.  
  60.       | Tree.VarDef: WITH Pattern^.VarDef DO
  61.         IF Object # NoTree THEN
  62.            WITH Object^.Formal DO
  63.           !      IF NOT (equal! ImplMod (TypeDesc); ! (! ImplMod (Path);
  64.           !, ! ImplMod (Pattern^.VarDef.Path); !)) THEN EXIT; END;!
  65.            END;
  66.         END;
  67.      END;
  68.  
  69.       | Tree.NilTest:
  70.      !      IF ! ImplMod (Pattern^.NilTest.Path); ! # NIL THEN EXIT; END;!
  71.  
  72.       | Tree.DontCare1:
  73.       | Tree.DontCare: RETURN;
  74.  
  75.       | Tree.Value: WITH Pattern^.Value DO
  76.         AssignTempo (Expr);
  77.         !      IF NOT (equal! ImplMod (Formals^.Formal.TypeDesc);
  78.         ! (! ImplMod (Path); !, ! Expression (Expr); !)) THEN EXIT; END;!
  79.         MatchExpr (Expr);
  80.      END;
  81.       END;
  82.       Match (t^.OnePattern.Next, Formals^.Formal.Next);
  83.    END Match;
  84.  
  85. PROCEDURE MatchExprs (t: tTree);
  86.    BEGIN
  87.       IF t^.Kind = Tree.NoExpr THEN RETURN; END;
  88.       IF t^.OneExpr.Expr^.Kind = Tree.DontCare THEN RETURN; END;
  89.       MatchExpr (t^.OneExpr.Expr);
  90.       MatchExprs (t^.OneExpr.Next);
  91.    END MatchExprs;
  92.  
  93. PROCEDURE MatchExpr (t: tTree);
  94.    BEGIN
  95.       CASE t^.Kind OF
  96.       | Tree.Compose:
  97.      MatchExprs (t^.Compose.Exprs);
  98.  
  99.       | Tree.VarUse    :
  100.       | Tree.Nil    :
  101.       | Tree.DontCare1    :
  102.       | Tree.TargetExpr    :
  103.       | Tree.StringExpr    :
  104.       | Tree.AttrDesc    :
  105.  
  106.       | Tree.Call    : WITH t^.Call DO
  107.         MatchExpr (Expr);
  108.         MatchExprs (Exprs);
  109.         IF Object # NoTree THEN
  110.            Match (Patterns, Object^.Routine.OutForm);
  111.         END;
  112.      END;
  113.  
  114.       | Tree.Binary    : WITH t^.Binary DO
  115.         MatchExpr (Lop);
  116.         MatchExpr (Rop);
  117.      END;
  118.  
  119.       | Tree.PreOperator, Tree.PostOperator    :
  120.      MatchExpr (t^.PreOperator.Expr);
  121.  
  122.       | Tree.Index    :
  123.      MatchExpr (t^.Index.Expr);
  124.      MatchExprs (t^.Index.Exprs);
  125.  
  126.       | Tree.Parents    :
  127.      MatchExpr (t^.Parents.Expr);
  128.       END;
  129.    END MatchExpr;
  130.  
  131. PROCEDURE AssignTempos (t: tTree);
  132.    BEGIN
  133.       IF t^.Kind = Tree.NoExpr THEN RETURN; END;
  134.       IF t^.OneExpr.Expr^.Kind = Tree.DontCare THEN RETURN; END;
  135.       AssignTempo (t^.OneExpr.Expr);
  136.       AssignTempos (t^.OneExpr.Next);
  137.    END AssignTempos;
  138.  
  139. PROCEDURE AssignTempo (t: tTree);
  140.    VAR TreeName    : tIdent;
  141.    BEGIN
  142.       CASE t^.Kind OF
  143.       | Tree.Compose: WITH t^.Compose DO
  144.         TreeName := Object^.Class.TypeDesc^.NodeTypes.TreeName^.TreeName.Name;
  145.         !      yyALLOC (! WI (TreeName); !,Make! WI (TreeName); !,! WI (Tempo); !,! WI (Object^.Class.Name); !)!
  146.         IF (Exprs^.Kind = Tree.OneExpr) AND (Exprs^.OneExpr.Expr^.Kind # Tree.DontCare) THEN
  147.            !      WITH ! WI (Tempo); !^.! WI (Object^.Class.Name); ! DO!
  148.            AssignFormals (Exprs, Object^.Class.Formals);
  149.            !      END;!
  150.         END;
  151.      END;
  152.  
  153.       | Tree.VarUse    :
  154.       | Tree.Nil    :
  155.       | Tree.DontCare1    :
  156.       | Tree.TargetExpr    :
  157.       | Tree.StringExpr    :
  158.       | Tree.AttrDesc    :
  159.  
  160.       | Tree.Call    : WITH t^.Call DO
  161.         AssignTempo (Expr);
  162.         AssignTempos (Exprs);
  163.      END;
  164.  
  165.       | Tree.Binary    : WITH t^.Binary DO
  166.         AssignTempo (Lop);
  167.         AssignTempo (Rop);
  168.      END;
  169.  
  170.       | Tree.PreOperator, Tree.PostOperator    :
  171.      AssignTempo (t^.PreOperator.Expr);
  172.  
  173.       | Tree.Index    :
  174.      AssignTempo (t^.Index.Expr);
  175.      AssignTempos (t^.Index.Exprs);
  176.  
  177.       | Tree.Parents    :
  178.      AssignTempo (t^.Parents.Expr);
  179.       END;
  180.    END AssignTempo;
  181.  
  182. PROCEDURE AssignFormals (t, Formals: tTree);
  183.    BEGIN
  184.       IF (t^.Kind = Tree.NoExpr) OR (Formals^.Kind # Tree.Formal) THEN RETURN; END;
  185.       IF t^.OneExpr.Expr^.Kind = Tree.DontCare THEN
  186.      BeginFormals (Formals);
  187.      RETURN;
  188.       END;
  189.       AssignFormal (t^.OneExpr.Expr, Formals);
  190.       AssignFormals (t^.OneExpr.Next, Formals^.Formal.Next);
  191.    END AssignFormals;
  192.  
  193. PROCEDURE AssignFormal (t, Formals: tTree);
  194.    VAR TreeName    : tIdent;
  195.    BEGIN
  196.       IF t^.Kind = Tree.Compose THEN
  197.      WITH t^.Compose DO
  198.         TreeName := Object^.Class.TypeDesc^.NodeTypes.TreeName^.TreeName.Name;
  199.         !      yyALLOC (! WI (TreeName); !,Make! WI (TreeName); !,! WI (Formals^.Formal.Name); !,! WI (Object^.Class.Name); !)!
  200.         IF (Exprs^.Kind = Tree.OneExpr) AND (Exprs^.OneExpr.Expr^.Kind # Tree.DontCare) THEN
  201.            !      WITH ! WI (Formals^.Formal.Name); !^.! WI (Object^.Class.Name); ! DO!
  202.            AssignFormals (Exprs, Object^.Class.Formals);
  203.            !      END;!
  204.         END;
  205.      END;
  206.       ELSE
  207.      AssignTempo (t);
  208.       END;
  209.  
  210.       CASE t^.Kind OF
  211.       | Tree.VarUse, Tree.Nil, Tree.Call, Tree.Binary, Tree.PreOperator,
  212.     Tree.PostOperator, Tree.Index, Tree.Parents, Tree.TargetExpr, Tree.StringExpr,
  213.     Tree.AttrDesc:
  214.      !      ! WI (Formals^.Formal.Name); ! := ! Expression (t); !;!
  215.       | Tree.DontCare1:
  216.      !      begin! ImplMod (Formals^.Formal.TypeDesc); ! (! WI (Formals^.Formal.Name); !)!
  217.       ELSE
  218.       END;
  219.    END AssignFormal;
  220.  
  221. PROCEDURE BeginFormals (Formals: tTree);
  222.    BEGIN
  223.       IF Formals^.Kind = Tree.Formal THEN
  224.      WITH Formals^.Formal DO
  225.         !      begin! ImplMod (TypeDesc); ! (! WI (Name); !)!
  226.         BeginFormals (Next);
  227.      END;
  228.       END;
  229.    END BeginFormals;
  230.  
  231. PROCEDURE ConsPatterns (t: tTree; ListCount: INTEGER): INTEGER;
  232.    BEGIN
  233.       IF t^.Kind = Tree.NoPattern THEN RETURN ListCount; END;
  234.       WITH t^.OnePattern DO
  235.      IF Pattern^.Kind = Tree.DontCare THEN
  236.         RETURN ConsTempos (Pattern^.DontCare.Tempos, ListCount);
  237.      ELSE
  238.         IF ListCount > 0 THEN !, ! END;
  239.         WI (Pattern^.Pattern.Tempo);
  240.         RETURN ConsPatterns (Next, ListCount + 1);
  241.      END;
  242.       END;
  243.    END ConsPatterns;
  244.  
  245. PROCEDURE ConsTempos (t: tTree; ListCount: INTEGER): INTEGER;
  246.    BEGIN
  247.       IF t^.Kind = Tree.Formal THEN
  248.      IF ListCount > 0 THEN !, ! END;
  249.      WI (t^.Formal.Name);
  250.      RETURN ConsTempos (t^.Formal.Next, ListCount + 1);
  251.       ELSE
  252.      RETURN ListCount;
  253.       END;
  254.    END ConsTempos;
  255.  
  256. PROCEDURE Expressions (t: tTree; ListCount: INTEGER): INTEGER;
  257.    BEGIN
  258.       IF t^.Kind = Tree.NoExpr THEN RETURN ListCount; END;
  259.       WITH t^.OneExpr DO
  260.      IF Expr^.Kind = Tree.DontCare THEN
  261.         RETURN ConsTempos (Expr^.DontCare.Tempos, ListCount);
  262.      ELSE
  263.         IF ListCount > 0 THEN !, ! END;
  264.         Expression (Expr);
  265.         RETURN Expressions (Next, ListCount + 1);
  266.      END;
  267.       END;
  268.    END Expressions;
  269.  
  270. PROCEDURE Expression (t: tTree);
  271.    BEGIN
  272.       CASE t^.Kind OF
  273.       | Tree.Compose    : WI (t^.Compose.Tempo);
  274.  
  275.       | Tree.Nil    : !NIL! 
  276.  
  277.       | Tree.VarUse    : WITH t^.VarUse DO
  278.         IF Object # NoTree THEN
  279.            ImplMod (Object^.Formal.Path);
  280.         ELSE
  281.            WI (Name);
  282.         END;
  283.      END;
  284.  
  285.       | Tree.DontCare1    : WI (t^.DontCare1.Tempo);
  286.  
  287.       | Tree.Call    : WITH t^.Call DO
  288.         Expression (Expr); ! (! 
  289.         ListCount := Expressions (Exprs, 0);
  290.         IF Object # NoTree THEN
  291.            ListCount := ConsPatterns (Patterns, ListCount);
  292.         ELSE
  293.            ListCount := Expressions (Patterns, ListCount);
  294.         END;
  295.         !)! 
  296.      END;
  297.  
  298.       | Tree.Binary    : WITH t^.Binary DO
  299.         Expression (Lop); ! ! WI (Operator); ! ! Expression (Rop);
  300.      END;
  301.  
  302.       | Tree.PreOperator    :
  303.      WI (t^.PreOperator.Operator); ! ! Expression (t^.PreOperator.Expr);
  304.  
  305.       | Tree.PostOperator    :
  306.      Expression (t^.PostOperator.Expr); ! ! WI (t^.PostOperator.Operator);
  307.  
  308.       | Tree.Index    :
  309.      Expression (t^.Index.Expr); ! [! ListCount := Expressions (t^.Index.Exprs, 0); !]! 
  310.  
  311.       | Tree.Parents    : !(! Expression (t^.Parents.Expr); !)! 
  312.  
  313.       | Tree.TargetExpr    : ImplMod (t^.TargetExpr.Expr);
  314.  
  315.       | Tree.StringExpr    : WriteString (f, t^.StringExpr.String);
  316.  
  317.       | Tree.AttrDesc    : WITH t^.AttrDesc DO
  318.         ImplMod (Object^.Formal.Path); !^.! WI (Type); !.! WI (Attribute);
  319.      END;
  320.       END;
  321.    END Expression;
  322.  
  323. PROCEDURE Tg2 (t, Formals: tTree);
  324.    BEGIN
  325.       IF (t^.Kind = Tree.NoPattern) OR (Formals^.Kind = Tree.NoFormal) THEN RETURN; END;
  326.       CASE t^.OnePattern.Pattern^.Kind OF
  327.       | Tree.Decompose:
  328.      !     WITH ! WI (Formals^.Formal.Name); !^.! WI (t^.OnePattern.Pattern^.Decompose.Object^.Class.Name); ! DO!
  329.       | Tree.VarDef, Tree.NilTest, Tree.Value, Tree.DontCare1:
  330.       ELSE RETURN;
  331.       END;
  332.       Tg2 (t^.OnePattern.Next, Formals^.Formal.Next);
  333.    END Tg2;
  334. }
  335.  
  336. PROCEDURE MacroMod (t: Tree)
  337.  
  338. Spec (..) :- {
  339.     MacroMod (TreeNames);
  340. }; .
  341. TreeName (..) :- {
  342.     !# define begint! WI (Name); !(a)    a := NIL;!
  343.     !# define equalt! WI (Name); !(a, b)    ! WI (Name); !.IsEqual! WI (Name); ! (a, b)!
  344.     MacroMod (Next);
  345. }; .
  346.  
  347.  
  348. PROCEDURE DefMod (t: Tree)
  349.  
  350. Spec (..) :- {
  351.     !DEFINITION MODULE ! WI (TrafoName); !;!
  352.     !!
  353.     !IMPORT SYSTEM, IO! DefMod (TreeNames); !;!
  354.     WriteLine (Codes^.Codes.ImportLine);
  355.     WriteText (f, Codes^.Codes.Import);
  356.     WriteLine (Codes^.Codes.ExportLine);
  357.     WriteText (f, Codes^.Codes.Export);
  358.     !!
  359.     !VAR yyf    : IO.tFile;!
  360.     !VAR Exit    : PROC;!
  361.     !!
  362.     DefMod (Public);
  363.     !!
  364.     !PROCEDURE Begin! WI (TrafoName); !;!
  365.     !PROCEDURE Close! WI (TrafoName); !;!
  366.     !!
  367.     !END ! WI (TrafoName); !.!
  368. }; .
  369. TreeName (..) :- {
  370.     !, ! WI (Name);
  371.     DefMod (Next);
  372. }; .
  373. Name (..) :- {
  374.     IF Object # NoTree THEN
  375.        ListCount := 0;
  376.        !PROCEDURE ! WI (Name); ! (! 
  377.        DefMod (Object^.Routine.InForm);
  378.        DefMod (Object^.Routine.OutForm);
  379.        !)! 
  380.        IF Object^.Kind = Tree.Predicate THEN
  381.           !: BOOLEAN! 
  382.        ELSIF Object^.Kind = Tree.Function THEN
  383.           !: ! DefMod (Object^.Function.ReturnForm^.Formal.TypeDesc);
  384.        END;
  385.        !;!
  386.     END;
  387.     DefMod (Next);
  388. }; .
  389. Formal (..) :- {
  390.     IF ListCount > 0 THEN !; ! END;
  391.     IF Path^.Var.IsOutput THEN !VAR ! END;
  392.     WI (Name); !: ! 
  393.     DefMod (TypeDesc);
  394.     INC (ListCount);
  395.     DefMod (Next);
  396. }; .
  397. NodeTypes (..) :- {
  398.     WI (TreeName^.TreeName.Name); !.t! WI (TreeName^.TreeName.Name);
  399. }; .
  400. UserType (..) :- {
  401.     WI (Type);
  402. }; .
  403.  
  404.  
  405. PROCEDURE ImplMod (t: Tree)
  406.  
  407. Spec (..) :- {
  408.     !IMPLEMENTATION MODULE ! WI (TrafoName); !;!
  409.       IF NOT IsElement (ORD ('m'), Options) THEN
  410.     !# define yyInline!
  411.       END;
  412.     !# ifdef yyInline!
  413.     !# define yyALLOC(tree, make, ptr, kind) \!
  414.     !  ptr := tree.yyPoolFreePtr; \!
  415.     !  IF SYSTEM.ADDRESS (ptr) >= tree.yyPoolMaxPtr THEN ptr := tree.yyAlloc (); END; \!
  416.     !  INC (tree.yyPoolFreePtr, tree.yyNodeSize [tree.kind]); \!
  417.     !  ptr^.yyHead.yyMark := 0; \!
  418.     !  ptr^.Kind := tree.kind;!
  419.     !# else!
  420.     !# define yyALLOC(tree, make, ptr, kind) ptr := tree.make (tree.kind);!
  421.     !# endif!
  422.     !!
  423.     !# define yyWrite(s) IO.WriteS (yyf, s)!
  424.     !# define yyWriteNl IO.WriteNl (yyf)!
  425.     !!
  426.     !IMPORT SYSTEM, System, IO! DefMod (TreeNames); !;!
  427.     WriteLine (Codes^.Codes.GlobalLine);
  428.     WriteText (f, Codes^.Codes.Global);
  429.     @# include "yy@ WI (TrafoName); @.w"@
  430.     !!
  431.     !PROCEDURE yyAbort (yyFunction: ARRAY OF CHAR);!
  432.     ! BEGIN!
  433.     !  IO.WriteS (IO.StdError, 'Error: module ! WI (TrafoName); !, routine ');!
  434.     !  IO.WriteS (IO.StdError, yyFunction);!
  435.     !  IO.WriteS (IO.StdError, ' failed');!
  436.     !  IO.WriteNl (IO.StdError);!
  437.     !  Exit;!
  438.     ! END yyAbort;!
  439.     !!
  440.     !PROCEDURE yyIsEqual (yya, yyb: ARRAY OF SYSTEM.BYTE): BOOLEAN;!
  441.     ! VAR yyi    : INTEGER;!
  442.     ! BEGIN!
  443.     !  FOR yyi := 0 TO INTEGER (HIGH (yya)) DO!
  444.     !   IF yya [yyi] # yyb [yyi] THEN RETURN FALSE; END;!
  445.     !  END;!
  446.     !  RETURN TRUE;!
  447.     ! END yyIsEqual;!
  448.     !!
  449.     ImplMod (Routines);
  450.     !PROCEDURE Begin! WI (TrafoName); !;!
  451.     ! BEGIN!
  452.     WriteLine (Codes^.Codes.BeginLine);
  453.     WriteText (f, Codes^.Codes.Begin);
  454.     ! END Begin! WI (TrafoName); !;!
  455.     !!
  456.     !PROCEDURE Close! WI (TrafoName); !;!
  457.     ! BEGIN!
  458.     WriteLine (Codes^.Codes.CloseLine);
  459.     WriteText (f, Codes^.Codes.Close);
  460.     ! END Close! WI (TrafoName); !;!
  461.     !!
  462.     !PROCEDURE yyExit;!
  463.     ! BEGIN!
  464.     !  IO.CloseIO; System.Exit (1);!
  465.     ! END yyExit;!
  466.     !!
  467.     !BEGIN!
  468.     ! yyf    := IO.StdOutput;!
  469.     ! Exit    := yyExit;!
  470.     ! Begin! WI (TrafoName); !;!
  471.     !END ! WI (TrafoName); !.!
  472. }; .
  473. Procedure (..) :- {
  474.     ListCount := 0;
  475.     !PROCEDURE ! WI (Name); ! (! 
  476.     DefMod (InForm);
  477.     DefMod (OutForm);
  478.     !);!
  479.     WriteLine (LocalLine);
  480.     WriteText (f, Local);
  481.     ! VAR yyTempo: RECORD CASE : INTEGER OF!
  482.     RoutineKind := kProcedure;
  483.     InFormals := InForm;
  484.     OutFormals := OutForm;
  485.     Declare (Rules);
  486.     ! END; END;!
  487.     ! BEGIN!
  488.       IF IsElement (ORD ('n'), Options) THEN
  489.     Tg1 (InForm);
  490.       END;
  491.       IF IsElement (ORD ('b'), Options) THEN
  492.     ImplMod (Rules);
  493.         IF IsElement (ORD ('f'), Options) THEN
  494.        !  yyAbort ('! WI (Name); !');!
  495.         END;
  496.       ELSE
  497.     TemposDone := FALSE;
  498.     CommonTestElim (Decisions);
  499.         IF IsElement (ORD ('f'), Options) AND NOT NeedsNoFinale (Decisions) THEN
  500.        !  yyAbort ('! WI (Name); !');!
  501.     END;
  502.       END;
  503.     ! END ! WI (Name); !;!
  504.     !!
  505.     ImplMod (Next);
  506. }; .
  507. Function (..) :- {
  508.     ListCount := 0;
  509.     !PROCEDURE ! WI (Name); ! (! 
  510.     DefMod (InForm);
  511.     DefMod (OutForm);
  512.     !): ! DefMod (ReturnForm^.Formal.TypeDesc); !;!
  513.     WriteLine (LocalLine);
  514.     WriteText (f, Local);
  515.     ! VAR yyTempo: RECORD CASE : INTEGER OF!
  516.     RoutineKind := kFunction;
  517.     InFormals := InForm;
  518.     OutFormals := OutForm;
  519.     ReturnFormals := ReturnForm;
  520.     Declare (Rules);
  521.     ! END; END;!
  522.     ! BEGIN!
  523.       IF IsElement (ORD ('b'), Options) THEN
  524.     ImplMod (Rules);
  525.     !  yyAbort ('! WI (Name); !');!
  526.       ELSE
  527.     TemposDone := FALSE;
  528.     CommonTestElim (Decisions);
  529.     IF NOT NeedsNoFinale (Decisions) THEN
  530.        !  yyAbort ('! WI (Name); !');!
  531.     END;
  532.       END;
  533.     ! END ! WI (Name); !;!
  534.     !!
  535.     ImplMod (Next);
  536. }; .
  537. Predicate (..) :- {
  538.     ListCount := 0;
  539.     !PROCEDURE ! WI (Name); ! (! 
  540.     DefMod (InForm);
  541.     DefMod (OutForm);
  542.     !): BOOLEAN;!
  543.     WriteLine (LocalLine);
  544.     WriteText (f, Local);
  545.     ! VAR yyTempo: RECORD CASE : INTEGER OF!
  546.     RoutineKind := kPredicate;
  547.     InFormals := InForm;
  548.     OutFormals := OutForm;
  549.     Declare (Rules);
  550.     ! END; END;!
  551.     ! BEGIN!
  552.       IF IsElement (ORD ('n'), Options) THEN
  553.     Tg1 (InForm);
  554.       END;
  555.       IF IsElement (ORD ('b'), Options) THEN
  556.     ImplMod (Rules);
  557.     !  RETURN FALSE;!
  558.       ELSE
  559.     TemposDone := FALSE;
  560.     CommonTestElim (Decisions);
  561.     IF NOT NeedsNoFinale (Decisions) THEN
  562.        !  RETURN FALSE;!
  563.     END;
  564.       END;
  565.     ! END ! WI (Name); !;!
  566.     !!
  567.     ImplMod (Next);
  568. }; .
  569. Rule (..) :- {
  570.     WriteLine (Line);
  571.     IF HasTempos THEN !    WITH yyTempo.yyR! WN (Index); ! DO! END;
  572.     !   LOOP!
  573.     Decls := VarDecls;
  574.     Match (Patterns, InFormals);
  575.       IF IsElement (ORD ('w'), Options) AND (Statements^.Kind # Tree.NoStatement) THEN
  576.     Tg2 (Patterns, InFormals);
  577.       END;
  578.     ImplMod (Statements);
  579.     IF NOT HasRejectOrFail THEN
  580.        AssignFormals (Exprs, OutFormals);
  581.        MatchExprs (Exprs);
  582.        CASE RoutineKind OF
  583.        | kProcedure: !      RETURN;!
  584.  
  585.        | kFunction :
  586.           AssignTempo (Expr);
  587.           IF HasPatterns AND (Expr^.Kind # Tree.Compose) AND (t^.Kind # Tree.DontCare1) THEN
  588.          !      ! WI (Tempo); ! := ! Expression (Expr); !;!
  589.          MatchExpr (Expr);
  590.          !      RETURN ! WI (Tempo); !;!
  591.           ELSE
  592.          MatchExpr (Expr);
  593.          !      RETURN ! Expression (Expr); !;!
  594.           END;
  595.  
  596.        | kPredicate: !      RETURN TRUE;!
  597.        END;
  598.     END;
  599.       IF IsElement (ORD ('w'), Options) AND (Statements^.Kind # Tree.NoStatement) THEN
  600.     Tg3 (Patterns);
  601.       END;
  602.     !   END;!
  603.     IF HasTempos THEN !    END;!
  604.     END;
  605.     !!
  606.     ImplMod (Next);
  607. }; .
  608. ProcCall (..) :- {
  609.     WriteLine (Pos);
  610.     AssignTempo (Call);
  611.     !      ! Expression (Call); !;!
  612.     MatchExpr (Call);
  613.     ImplMod (Next);
  614. }; .
  615. Condition (..) :- {
  616.     WriteLine (Pos);
  617.     AssignTempo (Expr);
  618.     !      IF NOT (! Expression (Expr); !) THEN EXIT; END;!
  619.     MatchExpr (Expr);
  620.     ImplMod (Next);
  621. }; .
  622. Assignment (..) :- {
  623.     WriteLine (Pos);
  624.     AssignTempo (Adr);
  625.     AssignTempo (Expr);
  626.     IF Object # NoTree THEN
  627.        !      ! ImplMod (Object^.Formal.Path);
  628.     ELSE
  629.        !      ! Expression (Adr);
  630.     END;
  631.     ! := ! Expression (Expr); !;!
  632.     MatchExpr (Adr);
  633.     MatchExpr (Expr);
  634.     ImplMod (Next);
  635. }; .
  636. Reject (..) :- {
  637.     WriteLine (Pos);
  638.     !      EXIT;!
  639. }; .
  640. Fail (..) :- {
  641.     WriteLine (Pos);
  642.     !      RETURN! IF RoutineKind = kPredicate THEN ! FALSE! END; !;!
  643. }; .
  644. TargetStmt (..) :- {
  645.     WriteLine (Pos);
  646.     !      ! ImplMod (Stmt); !;!
  647.     ImplMod (Next);
  648. }; .
  649. Nl (..) :- {
  650.     WriteLine (Pos);
  651.     !      yyWriteNl;!
  652.     ImplMod (Next);
  653. }; .
  654. WriteStr (..) :- {
  655.     WriteLine (Pos);
  656.     !      yyWrite (! WriteString (f, String); !);!
  657.     ImplMod (Next);
  658. }; .
  659. Ident (..) :- Var: tTree; {
  660.     Var := IdentifyVar (Decls, Attribute);
  661.     IF Var # NoTree THEN ImplMod (Var^.Formal.Path); ELSE WI (Attribute); END;
  662.     ImplMod (Next);
  663. }; .
  664. Any (..) :- {
  665.     WriteString (f, Code);
  666.     ImplMod (Next);
  667. }; .
  668. Anys (..) :- {
  669.     ImplMod (Layouts);
  670.     ImplMod (Next);
  671. }; .
  672. LayoutAny (..) :- {
  673.     WriteString (f, Code);
  674.     ImplMod (Next);
  675. }; .
  676. Designator (..) :- {
  677.     ImplMod (Object^.Formal.Path); !^.! WI (Type); !.! WI (Attribute);
  678.     ImplMod (Next);
  679. }; .
  680. Field (..) :- {
  681.     ImplMod (Next);
  682.     !.! WI (Name);
  683. }; .
  684. ConsType (..) :- {
  685.     ImplMod (Next);
  686.     !^.! WI (Name);
  687. }; .
  688. Var (..) :- {
  689.     WI (Name);
  690. }; .
  691. NodeTypes (..) :- {
  692.     !t! WI (TreeName^.TreeName.Name);
  693. }; .
  694. UserType (..) :- {
  695.     WI (Type);
  696. }; .
  697.  
  698.  
  699. PROCEDURE Declare (t: Tree)
  700.  
  701. Formal (..) :- {
  702.     !  ! WI (Name); !: ! DefMod (TypeDesc); !;!
  703.     Declare (Next);
  704. }; .
  705. Param (..) :- Var: tTree; {
  706.     Var := IdentifyVar (Decls, Name);
  707.     !  ! WI (Name); !: ! DefMod (Var^.Formal.TypeDesc); !;!
  708.     Declare (Next);
  709. }; .
  710. Rule (..) :- {
  711.     IF HasTempos THEN
  712.        ! | ! WN (Index); !: yyR! WN (Index); !: RECORD!
  713.        Decls := VarDecls;
  714.        Declare (Patterns);
  715.        Declare (Exprs);
  716.        IF RoutineKind = kFunction THEN
  717.           Declare (Expr);
  718.           IF HasPatterns AND (Expr^.Kind # Tree.Compose) AND (t^.Kind # Tree.DontCare1) THEN
  719.          !  ! WI (Tempo); !: ! DefMod (ReturnFormals^.Formal.TypeDesc); !;!
  720.           END;
  721.        END;
  722.        Declare (Statements);
  723.        !  END;!
  724.     END;
  725.     Declare (Next);
  726. }; .
  727. ProcCall (..) :- {
  728.     Declare (Call);
  729.     Declare (Next);
  730. }; .
  731. Condition (..) :- {
  732.     Declare (Expr);
  733.     Declare (Next);
  734. }; .
  735. Assignment (..) :- {
  736.     Declare (Adr);
  737.     Declare (Expr);
  738.     Declare (Next);
  739. }; .
  740. TargetStmt (..) :- {
  741.     Declare (Parameters);
  742.     Declare (Next);
  743. }; .
  744. Statement (..) :- {
  745.     Declare (Next);
  746. }; .
  747. OnePattern (..) :- {
  748.     IF (Pattern^.Pattern.Tempo # NoIdent) AND (Pattern^.Kind # Tree.DontCare1) THEN
  749.        !  ! WI (Pattern^.Pattern.Tempo); !: ! DefMod (Pattern^.Pattern.TypeDesc); !;!
  750.     END;
  751.     Declare (Pattern);
  752.     Declare (Next);
  753. }; .
  754. OneExpr (..) :- {
  755.     Declare (Expr);
  756.     Declare (Next);
  757. }; .
  758. Decompose (..) :- {
  759.     Declare (Patterns);
  760. }; .
  761. DontCare (..) :- {
  762.     Declare (Tempos);
  763. }; .
  764. DontCare1 (..) :- {
  765.     IF Tempo # NoIdent THEN
  766.        !  ! WI (Tempo); !: ! DefMod (TypeDesc); !;!
  767.     END;
  768. }; .
  769. Value (..) :- {
  770.     Declare (Expr);
  771. }; .
  772. Compose (..) :- {
  773.     IF Tempo # NoIdent THEN
  774.        !  ! WI (Tempo); !: ! DefMod (TypeDesc); !;!
  775.     END;
  776.     Declare (Exprs);
  777. }; .
  778. Call (..) :- {
  779.     Declare (Expr);
  780.     Declare (Exprs);
  781.     Declare (Patterns);
  782. }; .
  783. Binary (..) :- {
  784.     Declare (Lop);
  785.     Declare (Rop);
  786. }; .
  787. PreOperator (..) ;
  788. PostOperator (..) ;
  789. Parents (..) :- {
  790.     Declare (Expr); 
  791. }; .
  792. Index (..) :- {
  793.     Declare (Expr);
  794.     Declare (Exprs);
  795. }; .
  796.  
  797.  
  798. PROCEDURE Tg1 (t: Tree)
  799.  
  800. Formal (..) :- {
  801.     TheName := Name;
  802.     Tg1 (TypeDesc);
  803.     Tg1 (Next);
  804. }; .
  805. NodeTypes (..) :- {
  806.     !  IF ! WI (TheName); ! = ! WI (TreeName^.TreeName.Name); !.No! 
  807.     WI (TreeName^.TreeName.Name); ! THEN RETURN! IF RoutineKind = kPredicate THEN ! FALSE! END; !; END;!
  808. }; .
  809.  
  810.  
  811. PROCEDURE Tg3 (t: Tree)
  812.  
  813. OnePattern (..) :- {
  814.     Tg3 (Pattern);
  815.     Tg3 (Next);
  816. }; .
  817. Decompose (..) :- {
  818.     !     END;!
  819. }; .
  820.  
  821. PROCEDURE CommonTestElim2 (OneTest: OneTest, Decisions)
  822.  
  823. TestValue (..), Then :-
  824.    (NeedsMatch (Expr));
  825.    CommonTestElim (OneTest);
  826.    !    LOOP!
  827.    MatchExpr (OneTest^.TestValue.Expr);
  828.    CommonTestElim (Then);
  829.    !    EXIT; END;!
  830.    .
  831. _, Then :-
  832.    CommonTestElim (OneTest);
  833.    CommonTestElim (Then);
  834.    .
  835.  
  836. PROCEDURE CommonTestElim (t: Tree)
  837.  
  838. Decision (..) :- {
  839.     IF Cases = 0 THEN
  840.        IF NOT TemposDone AND (OneTest^.Kind = Tree.TestValue) AND NeedsTempo (Then, rule) THEN
  841.           !    WITH yyTempo.yyR! WN (rule^.Rule.Index); ! DO!
  842.           TemposDone := TRUE;
  843.           CommonTestElim2 (OneTest, Then);
  844.           !  END;!
  845.           !    END;!
  846.        ELSE
  847.           GetRule (Then, rule);
  848.           Decls := rule^.Rule.VarDecls;
  849.           CommonTestElim2 (OneTest, Then);
  850.           !  END;!
  851.        END;
  852.        TemposDone := FALSE;
  853.        CommonTestElim (Else);
  854.     ELSE
  855.        i := Cases; Case (t);
  856.     END;
  857. }; .
  858. Decided (..) :- {
  859.     CommonTestElim (Rule);
  860.     IF Rule^.Rule.HasExit THEN
  861.        TemposDone := FALSE;
  862.        CommonTestElim (Else);
  863.     END;
  864. }; .
  865. TestKind (..) :- {
  866.     !  IF (! ImplMod (Path); !^.Kind = ! 
  867.     WI (TypeDesc^.NodeTypes.TreeName^.TreeName.Name); !.! WI (Name); !) THEN!
  868. }; .
  869. TestIsType (..) :- {
  870.     !  IF ! WI (TypeDesc^.NodeTypes.TreeName^.TreeName.Name); !.IsType (! ImplMod (Path); !, ! 
  871.     WI (TypeDesc^.NodeTypes.TreeName^.TreeName.Name); !.! WI (Name); !) THEN!
  872. }; .
  873. TestNil (..) :- {
  874.     !  IF ! ImplMod (Path); ! = NIL THEN!
  875. }; .
  876. TestNonlin (..) :- {
  877.     !  IF (equal! ImplMod (TypeDesc); ! (! ImplMod (Path);
  878.     !, ! ImplMod (Path2); !)) THEN!
  879. }; .
  880. TestValue (..) :- {
  881.     AssignTempo (Expr);
  882.     !  IF (equal! ImplMod (TypeDesc); ! (! ImplMod (Path);
  883.     !, ! Expression (Expr); !)) THEN!
  884. }; .
  885. Rule (..) :- {
  886.     WriteLine (Line);
  887.     IF HasTempos AND NOT TemposDone THEN !    WITH yyTempo.yyR! WN (Index); ! DO!
  888.     END;
  889.     IF HasExit THEN !   LOOP!
  890.     END;
  891.     Decls := VarDecls;
  892.       IF IsElement (ORD ('w'), Options) AND (Statements^.Kind # Tree.NoStatement) THEN
  893.     Tg2 (Patterns, InFormals);
  894.       END;
  895.     ImplMod (Statements);
  896.     IF NOT HasRejectOrFail THEN
  897.        AssignFormals (Exprs, OutFormals);
  898.        MatchExprs (Exprs);
  899.        CASE RoutineKind OF
  900.        | kProcedure: !      RETURN;!
  901.  
  902.        | kFunction :
  903.           AssignTempo (Expr);
  904.           IF HasPatterns AND (Expr^.Kind # Tree.Compose) AND (t^.Kind # Tree.DontCare1) THEN
  905.          !      ! WI (Tempo); ! := ! Expression (Expr); !;!
  906.          MatchExpr (Expr);
  907.          !      RETURN ! WI (Tempo); !;!
  908.           ELSE
  909.          MatchExpr (Expr);
  910.          !      RETURN ! Expression (Expr); !;!
  911.           END;
  912.  
  913.        | kPredicate: !      RETURN TRUE;!
  914.        END;
  915.     END;
  916.       IF IsElement (ORD ('w'), Options) AND (Statements^.Kind # Tree.NoStatement) THEN
  917.     Tg3 (Patterns);
  918.       END;
  919.     IF HasExit THEN !   END;!
  920.     END;
  921.     IF HasTempos AND NOT TemposDone THEN !    END;!
  922.     END;
  923.     !!
  924. }; .
  925.  
  926.  
  927. PROCEDURE Case (t: Tree)    /* reads GLOBAL i */
  928.  
  929. Decision (..) :- n: CARDINAL; {
  930.     !!
  931.     !  CASE ! ImplMod (OneTest^.OneTest.Path); !^.Kind OF!
  932.     n := i;
  933.     WHILE n > 0 DO
  934.        IF NOT IsEmpty (t^.Decision.OneTest^.TestIsType.TypeDesc^.NodeTypes.Types) THEN
  935.           Case (t^.Decision.OneTest);
  936.           CommonTestElim (t^.Decision.Then);
  937.        END;
  938.        t := t^.Decision.Else;
  939.        DEC (n);
  940.     END;
  941.     !  ELSE END;!
  942.     !!
  943.     CommonTestElim (t);
  944. }; .
  945. TestKind (..) :- {
  946.     !  | ! WI (TypeDesc^.NodeTypes.TreeName^.TreeName.Name); !.! WI (Name); !:!
  947. }; .
  948. TestIsType (..) :- {
  949.     WITH TypeDesc^.NodeTypes DO
  950.        TheClass := LookupClass (TreeName^.TreeName.Classes, Minimum (Types));
  951.        !  | ! WI (TreeName^.TreeName.Name); !.! WI (TheClass^.Class.Name);
  952.        Case (TypeDesc); !:!
  953.     END;
  954. }; .
  955. NodeTypes (..) :- {
  956.     FOR j := Minimum (Types) + 1 TO Maximum (Types) DO
  957.        IF IsElement (j, Types) THEN
  958.           TheClass := LookupClass (TreeName^.TreeName.Classes, j); !!
  959.           !  , ! WI (TreeName^.TreeName.Name); !.! WI (TheClass^.Class.Name);
  960.        END;
  961.     END;
  962. }; .
  963.